package org.yuci.concurency.semaphore;

import java.util.concurrent.Semaphore;

public class Employer {

    private static final Semaphore workPermit = new Semaphore(1); 
    
    public void employ(Worker worker) {        
        if (workPermit.tryAcquire()) {
            // The work permit is available, which means:
            // 1. No one has been employed yet, or
            // 2. The previous worker has quit the job
            
            // Then go ahead employ the worker to do the job
            worker.setWorkPermit(workPermit);
            Thread thread = new Thread(worker);
            thread.start();
        } else {
            // The work permit has been issue to someone doing the job            
        }
    }
}
